//
//  MYGCloudDiskRenameFileAlert.swift
//  MYDoctor
//
//  Created by Denny on 2024/3/12.
//

import UIKit

class MYGCloudDiskRenameFileAlert: UIView {
    var type = 1 //修改类型（默认1修改文件名称 传2修改文件夹名称）
    var pid = ""//给文件夹创建子文件夹
    var isPatientImage = false //随身影像
    var is_sharing = false//是否是新建共享文件夹
    @IBOutlet weak var titleLB: UILabel!
    var tapClosePageCompletion:(()->Void)?
    var tapSureCompletion:((MYCloudDiskContentModel)->Void)?

    @IBOutlet weak var contentTF: UITextField!
    @IBOutlet weak var sureBtn: UIButton!
    var isRename = false
    var fileModel = MYCloudDiskContentModel()
    override func awakeFromNib() {
        super.awakeFromNib()
        sureBtn.addCAGradientLayerColor(colors: [UIColor.init(hexString: "#97B3FF").cgColor,
                                                 UIColor.init(hexString: "#4975EC").cgColor],
                                        startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint.init(x: 1, y: 0))
    }
    
    
    @IBAction func tapSure(_ sender: Any) {
        if self.contentTF.text?.count == 0 {return}
        self.endEditing(true)
        if  isPatientImage {
            patientImageEditName()
        }else {
            if isRename {
                editFileName()
            }else {
                addFileName()
            }
        }
   
    }
    
    func editFileName() {
        MYGLoading.show()
        MYGNetwork<MYGMeAPI,MYGEmptyModel>.request(target: .editNetDiskFolder(param: ["name": self.contentTF.text ?? "","type":"\(type)"], id: self.fileModel.id)) {[weak self] item in
            NotificationCenter.default.post(name: NSNotification.Name("MYCloudDiskViewController_configDataSource"), object: nil)
            MYGLoading.hide()
            self?.dismissSelf()
        } failed: { _, msg in
            MMYGHUD.showToast(text: msg)
            MYGLoading.hide()
        }
    }
    
    
    func addFileName() {
        MYGLoading.show()
        var param = ["name": self.contentTF.text ?? ""]
        if pid.count > 0 {
            param["pid"] = pid
        }
        if is_sharing {
            param["is_sharing"] = "1"
        }
        MYGNetwork<MYGMeAPI,MYCloudDiskContentModel>.request(target: .addNetDiskFolder(param: param)) {[weak self] item in
            NotificationCenter.default.post(name: NSNotification.Name("MYCloudDiskViewController_configDataSource"), object: nil)
            MYGLoading.hide()
            if let c = self?.tapSureCompletion {
                c(item)
            }
        } failed: { _, msg in
            MMYGHUD.showToast(text: msg)
            MYGLoading.hide()
        }
    }
    
    
    func patientImageEditName() {
        MYGLoading.show()
        MYGNetwork<MYGPatientImageApi, MYGEmptyModel>.request(target: .renameImageExamination(id: self.fileModel.id, name: self.contentTF.text ?? "")) {[weak self] item in
             MYGLoading.hide()
            NotificationCenter.default.post(name: NSNotification.Name("MYGpatientImageListVC_configDataSource"), object: nil)
            if let c = self?.tapClosePageCompletion {
                c()
            }

        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
    }
    func dismissSelf() {
        if let cc = self.tapClosePageCompletion  {
            cc()
        }
    }
    
    @IBAction func tapClose(_ sender: Any) {
        if let cc = self.tapClosePageCompletion {
            cc()
        }
    }
}
